<template>
    <div class="login_con">
        <div class="login_box">
            <!-- // img -->
            <div class="avatar_box">
                <img src="../assets/logo.png" alt="">
            </div>
            <!-- //form -->
            <el-form ref="loginFormRef" :model="loginForm" :rules="loginFormRules" label-width="90px" class="login_form">
                <el-form-item label="账号" prop="username">
                    <el-input v-model="loginForm.username" prefix-icon="el-icon-user"></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <el-input v-model="loginForm.password" prefix-icon="el-icon-search"
                    type="password"></el-input>
                </el-form-item>
                <!-- botton -->
                <el-form-item class="btns">
                <el-button type="primary" @click="login">登录</el-button>
                <el-button type="info" @click="reset">重置</el-button>
                <el-button type="primary" @click="regsiter">注册</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>
<script>

export default {
   name:"Login",
   data(){
       return {
           loginForm:{
               username:"cw",
               password:'1234567',
           },
           loginFormRules: {
            username: [
                { required: true, message: '请输入用户名', trigger: 'blur' },
                { min: 2, max: 8, message: '长度在 2 到 8 个字符', trigger: 'blur' }
                ],
            password:[
                 { required: true, message: '请输入密码', trigger: 'blur' },
                { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
                
            ]
           }
        }
   },
   methods:{
       reset(){
        //    console.log(this)
        this.$refs.loginFormRef.resetFields();
       },
       login(){
           //效验参数是否有效哦
           this.$refs.loginFormRef.validate(async (valid)=>{
               if (!valid){ //无效
                    return
               }
               const {data:res}=await this.$http.post("login",this.loginForm)
               
               if (res.code!==0){
                return this.$message.error("登录失败:"+res.msg); 
               }
               this.$message.success("登录成功")
               //接着就要保存token了，保存到sessionStorage
               //通过编程式导航跳转后台主页，路由地址是/home
               window.sessionStorage.setItem('token',res.data.token)
               window.sessionStorage.setItem('authorityId',res.data.user.authorityId)
               this.$router.push({path:'/home'})
           })
       },
       regsiter(){
            this.$router.push({path:'/register'})
       }
   }
}
</script>
<style lang="less" scoped>
.login_con{
    background-color: rgb(63, 88, 141);
    height: 100%;
}
.login_box{
    width: 450px;
    height: 300px;
    background-color: #fff;
    border-radius: 3px;
    position: absolute;
    left: 50%;
    top:50%;
    transform: translate(-50%,-50%);

}
.avatar_box{
    height: 130px;
    width: 130px;
    border: 1px solid #eee;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 0 10 #ddd;
    position: absolute;
    left: 50%;
    transform: translate(-50%,-50%);
    img{
        width: 100%;
        height: 100%;
        border-radius: 50%;
        background-color: #eee;
    }
}
.btns{
    display: flex;
    justify-content: flex-end;
}
.login_form{
    position: absolute;
    bottom: 0;
    width: 100%;
    padding: 0 10px;
    transform: translate(-5%);
}
</style>